<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="controller.CustomerController"%>
<%@page import="model.database.DBException"%>
<%@page import="controller.CommonValues"%>
<%@page import="view.WebCommons"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="customerData" class="view.useBeans.CustomerData"
	scope="page" />
<jsp:setProperty name="customerData" property="*" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update customer details</title>
</head>
<body>

	<span class="customHeader2">Update customer details</span>

	<%
		// extract controller from session		
		CustomerController controller = (CustomerController) session.getAttribute(WebCommons.CONTROLLER);

		// check if first time on page
		if (!customerData.isDataSet()) {

			// extract customer number from session
			Integer customerNumber = (Integer) session.getAttribute(WebCommons.CUSTOMER_NUMBER);

			// get customer's current details
			Map<String, String> customerDetails = null;
			try {

				customerDetails = controller.getCustomerDetails(customerNumber);
			} catch (DBException e) {

				// move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");
			}

			// check if customer exists
			if (customerDetails.isEmpty()) {

				// customer does not exist- move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");

			} else {

				// get customer's new details and display current details
				try {
					// get customer's most current details
					customerDetails = controller.getCustomerDetails(customerNumber);
				} catch (DBException e) {

					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}

				// display form to update details
				%>

				<div id="page">
					<div class="post">
			
						<div id="sidebar">
							<ul>
								<li>
									<h2>Please insert new personal details</h2>
								</li>
							</ul>
						</div>
						<div id="content">
							<form METHOD=POST ACTION="UpdateCustomerDetails.jsp">
								<table>
									<tr>
										<td><span class="customFormValue">Number</span></td>
										<td><INPUT TYPE=TEXT NAME=number
											VALUE=<%=customerDetails.get(CommonValues.NUMBER)%>
											SIZE=<%=WebCommons.NUMBER_SIZE%> readonly="readonly" class="formROdata"></td>
									</tr>
									<tr>
										<td><span class="customFormValue">Username</span></td>
										<td><INPUT TYPE=TEXT NAME=userName
											VALUE="<%=customerDetails.get(CommonValues.USER_NAME)%>"
											SIZE=<%=WebCommons.NAME_SIZE%> readonly="readonly" class="formROdata"></td>
									</tr>
									<tr>
										<td><span class="customFormValue">Name</span></td>
										<td><INPUT TYPE=TEXT NAME=name
											VALUE="<%=customerDetails.get(CommonValues.NAME)%>"
											SIZE=<%=WebCommons.NAME_SIZE%>></td>
									</tr>
									<tr>
										<td><span class="customFormValue">Address</span></td>
										<td><INPUT TYPE=TEXT NAME=address
											VALUE="<%=customerDetails.get(CommonValues.ADDRESS)%>"
											SIZE=<%=WebCommons.ADDRESS_SIZE%>></td>
									</tr>
									<tr>
										<td><span class="customFormValue">Credit Card</span></td>
										<td><INPUT TYPE=TEXT NAME=paymentMethod
											VALUE="<%=customerDetails.get(CommonValues.PAYMENT_METHOD)%>"
											SIZE=<%=WebCommons.NAME_SIZE%>></td>
									</tr>
								</table>
			
								<br> <INPUT TYPE=SUBMIT VALUE="update details"
									class="mySubmitButton">
							</form>
						</div>
					</div>
				</div>
				
				<%
				// return to main menu
				String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
				%>
				<div id="footer">
					<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back to Menu</A></span>
				</div>
				<%
			}

			// check if already submitted details
		} else {

			// extract given details
			int number = customerData.getCustomerNumberValue();
			String name = customerData.getName();
			String userName = customerData.getUserName();
			String address = customerData.getAddress();
			String creditCard = customerData.getPaymentMethod();

			try {

				// try updating customer
				boolean couldUpdate = controller.updateCustomerDetails(
						number, name, address, creditCard);

				// check success
				if (!couldUpdate) {

					/* if reached here, then customer number exists.
					 * therefore, if could not update then an error has occurred-
					 * move to error page
					 */
					response.sendRedirect("../../commonPages/ErrorPage.jsp");

				} else {

					// success

					// display customer's new details
					Map<String, String> customerDetails = null;
					try {
						// get customer's most current details
						customerDetails = controller.getCustomerDetails(customerData.getCustomerNumberValue());
					} catch (DBException e) {

						// move to error page
						response.sendRedirect("../../commonPages/ErrorPage.jsp");
					}

					// display form with updated details
					%>

					<div id="page">
						<div class="post">
				
							<div id="sidebar">
								<ul>
									<li>
										<h2>Please insert new personal details</h2>
									</li>
								</ul>
							</div>
							<div id="content">
								<form METHOD=POST ACTION="UpdateCustomerDetails.jsp">
									<table>
										<tr>
											<td><span class="customFormValue">Number</span></td>
											<td><INPUT TYPE=TEXT NAME=number
												VALUE=<%=customerDetails.get(CommonValues.NUMBER)%>
												SIZE=<%=WebCommons.NUMBER_SIZE%> readonly="readonly" class="formROdata"></td>
										</tr>
										<tr>
											<td><span class="customFormValue">Username</span></td>
											<td><INPUT TYPE=TEXT NAME=userName
												VALUE="<%=customerDetails.get(CommonValues.USER_NAME)%>"
												SIZE=<%=WebCommons.NAME_SIZE%> readonly="readonly" class="formROdata"></td>
										</tr>
										<tr>
											<td><span class="customFormValue">Name</span></td>
											<td><INPUT TYPE=TEXT NAME=name
												VALUE="<%=customerDetails.get(CommonValues.NAME)%>"
												SIZE=<%=WebCommons.NAME_SIZE%>></td>
										</tr>
										<tr>
											<td><span class="customFormValue">Address</span></td>
											<td><INPUT TYPE=TEXT NAME=address
												VALUE="<%=customerDetails.get(CommonValues.ADDRESS)%>"
												SIZE=<%=WebCommons.ADDRESS_SIZE%>></td>
										</tr>
										<tr>
											<td><span class="customFormValue">Credit Card</span></td>
											<td><INPUT TYPE=TEXT NAME=paymentMethod
												VALUE="<%=customerDetails.get(CommonValues.PAYMENT_METHOD)%>"
												SIZE=<%=WebCommons.NAME_SIZE%>></td>
										</tr>
									</table>
				
									<br> <INPUT TYPE=SUBMIT VALUE="update details"
										class="mySubmitButton">
								</form>
							</div>
						</div>
					</div>
					
					<%
					// return to main menu
					String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
					%>
					<div id="footer">
						<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
								to Menu</A></span>
					</div>
					<!-- notify success -->
					<span class="notify"><br>Updated successfully!</span>
			<%
				}
	
			} catch (DBException e) {

				// move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");
			}
		}		
	%>

</body>
</html>